package cn.nexd.app.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;

import cn.nexd.app.NexdApplication;
import cn.nexd.app.activity.inter.ILoadingActivityView;
import cn.nexd.app.arsenal.R;
import cn.nexd.app.perimission.LoadingActivityPermissionsDispatcher;
import cn.nexd.app.presenter.ILoadingPresenterImpl;
import cn.nexd.app.presenter.inter.ILoadingPresenter;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * 欢迎页面 , 用来显示公司的logo
 * <p/>
 * 采集工具 欢迎页面
 * <p/>
 * 业务逻辑:
 * 1.从本地读取用户信息
 * 2.从本地读取商场列表信息
 * <p/>
 * 特殊处理:
 * <p/>
 * 网络请求,更新本地USER & MALL, success : 跟新本地数据, false: 判断本地是否有数据, 没有,不能用,有,先用着.
 */
@RuntimePermissions
public class LoadingActivity extends Activity implements ILoadingActivityView {
    private ILoadingPresenter iLoadingPresenter;
    private ImageView animImg;
    private ImageView logoImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AnalyticsConfig.enableEncrypt(true);
        setContentView(R.layout.activity_welcom);
        animImg = (ImageView) findViewById(R.id.loading_anim_img);
        logoImg = (ImageView) findViewById(R.id.show_logo_image_view);
        iLoadingPresenter = new ILoadingPresenterImpl(this, this);
        LoadingActivityPermissionsDispatcher.showLocationWithCheck(this);
        if (NexdApplication.selectedMallModel != null) {
            NexdApplication.selectedMallModel = null;
        }
    }


    @NeedsPermission(Manifest.permission_group.LOCATION)
    public void locationCurrentPermission() {
        iLoadingPresenter.startLocation();
        LoadingActivityPermissionsDispatcher.showStorgeWithCheck(this);
    }

    @TargetApi(Build.VERSION_CODES.M)
    @NeedsPermission(Manifest.permission_group.PHONE)
    public void uploadOperaRecord() {
        // 先上传本地为上传的所有操作记录
        iLoadingPresenter.uploadOperaRecord();
    }

    @NeedsPermission(Manifest.permission_group.STORAGE)
    public void checkPhonePermission() {
        LoadingActivityPermissionsDispatcher.showPhoneWithCheck(this);
    }


    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // NOTE: delegate the permission handling to generated method
        LoadingActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }


    /**
     * 关于存储卡的权限请求
     */
    @OnPermissionDenied(Manifest.permission_group.STORAGE)
    public void onStorageDenied() {
        // NOTE: Deal with a denied permission, e.g. by showing specific UI
        // or disabling certain functionality
        showToast(getString(R.string.permission_sdcard_denied));
    }

    @OnNeverAskAgain(Manifest.permission_group.STORAGE)
    public void onStorageNeverAskAgain() {
        showToast(getString(R.string.permission_camera_never_askagain));
    }

    @OnShowRationale(Manifest.permission_group.STORAGE)
    public void showRationaleForStorage(PermissionRequest request) {
        // NOTE: Show a rationale to explain why the permission is needed, e.g. with a dialog.
        // Call proceed() or cancel() on the provided PermissionRequest to continue or abort
        showRationaleDialog(R.string.permission_sdcard_rationale, request);
    }

    @OnPermissionDenied(Manifest.permission_group.PHONE)
    public void onPhoneDenied() {
        showToast(getString(R.string.permission_phone_denied));
    }

    @OnNeverAskAgain(Manifest.permission_group.PHONE)
    public void onPhoneNeverAskAgain() {
        showToast(getString(R.string.permission_phone_never_askagain));
    }

    @OnShowRationale(Manifest.permission_group.PHONE)
    public void showRationaleForPhone(PermissionRequest permissionRequest) {
        showRationaleDialog(R.string.permission_phone_rationale, permissionRequest);
    }

    /**
     * 关于定位的权限请求
     */
    @OnPermissionDenied(Manifest.permission_group.LOCATION)
    public void onLocationDenied() {
        showToast(getString(R.string.permission_location_denied));
    }

    @OnNeverAskAgain(Manifest.permission_group.STORAGE)
    public void onLocationNeverAskAgain() {
        showToast(getString(R.string.permission_location_never_askagain));
    }

    @OnShowRationale(Manifest.permission_group.STORAGE)
    public void showRationaleForLocation(PermissionRequest request) {
        showRationaleDialog(R.string.permission_location_rationale, request);
    }


    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    private void showRationaleDialog(@StringRes int messageResId, final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(messageResId)
                .show();
    }
}
